package myJs.projectCps.oplsda2D

import com.softwaremill.quicklens._
import myJs.cps._
import myJs.tool.Tool._
import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.api.Api
import myJs.tool.Tool
import myJs.user.FeatureSelectionTool
import shared.pojo.MissionTool.CheckFileExist

import scala.collection.immutable.SeqMap

/** Created by yz on 3/11/2020
  */
object LayoutCp {

  val plotO = Cp.plotO

  case class Props(
      basicData: plotO.LayoutData,
      basicDataChange: (plotO.LayoutData => plotO.LayoutData) => SyncIO[Unit],
      id:Int,
      idStr:String
  ) {
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val (varNameStr, setVarNameStr) = useState("")

    object FTool {

      def vChange[T](f: PathLazyModify[plotO.LayoutData, T])(g: String => T) = (v: String) => {
        props.basicDataChange {
          f.setTo(g(v))
        }
      }

      def strChange(f: PathLazyModify[plotO.LayoutData, String]) = FTool.vChange(f)(x => x)

      def varNameStrChange = (v: String) => {
        setVarNameStr(v)
      }

    }

    val yNMap = SeqMap("T" -> messages("yes"), "F" -> messages("no"))
    val basicData = props.basicData
    val id = props.id
    val idStr = props.idStr

    Fragment(
      SelectInlineCp
        .Props(
          value = basicData.showSample,
          label = messages("showSampleNames"),
          name = s"showSample",
          optionMap = yNMap,
          onChange = FTool.strChange(modifyLens[plotO.LayoutData](_.showSample)),
          labelWidth = 4,
          inputWidth = 4
        )
        .render,
      SelectInlineCp
        .Props(
          value = basicData.showEllipse,
          label = messages("showConfidenceEllipse"),
          name = s"showEllipse",
          optionMap = yNMap,
          onChange = FTool.strChange(modifyLens[plotO.LayoutData](_.showEllipse)),
          labelWidth = 4,
          inputWidth = 4
        )
        .render,
      BetweenInlineCp
        .Props(
          label = messages("picSizeWidthHeight"),
          startName = "width",
          start = basicData.width,
          startChange = FTool.strChange(modifyLens[plotO.LayoutData](_.width)),
          endName = "height",
          end = basicData.height,
          endChange = FTool.strChange(modifyLens[plotO.LayoutData](_.height))
        )
        .render
    )

  }

}
